package spolipar.bitcoin.bot.data.bitstamp;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import spolipar.bitcoin.bot.S;
import spolipar.bitcoin.bot.data.DataFetcher;
import spolipar.bitcoin.bot.data.DeepData;
import spolipar.bitcoin.bot.data.IMarketData;
import spolipar.bitcoin.bot.data.MarketDataType;
import spolipar.bitcoin.bot.data.StorageKey;
import spolipar.bitcoin.bot.data.formatter.DeepStringFormatter;

import com.google.gson.Gson;

public class BitstampDataFetcher extends DataFetcher {
	//const
    private static final String GET="GET";
    private static final String PUT="PUT";
    private static final String POST="POST";
    private static final String ATTR_CONTENT_TYPE = "Content-Type";
    private static final String BINARY = "binary/octet-stream";
    private static final String PATH_DELIMITER = "/";

    private static final int HTTP_OK = 200;
    private static final int HTTP_NO_CONTENT = 204;//OK for Delete
    private static final int HTTP_NOT_FOUND = 404;//Not found
    

	private final static String URL = "https://bitstamp.net";

	private static String cid;
	private static String k;
	private static String s;
	
	@Override public StorageKey sotrageKey() { return StorageKey.BITSTAMP_MD;	}
	
	public IMarketData fetchData(MarketDataType type) throws IOException {
		IMarketData result = null;
		switch (type) {
		case DEEP:
			result = getDeep();
			break;
		default:
			throw new IOException("Not supported type" + type);
		}
		return result;
	}
	
	public DeepData getDeep() throws IOException {
		String url = URL + "/api/order_book/";
		HttpURLConnection conn = openHttpConnection(url);
		conn.setRequestMethod(GET);
        conn.setRequestProperty(ATTR_CONTENT_TYPE, "");
                   
        int code = -1;
        code = conn.getResponseCode();
        S.log("S3: Response code during downloading = " + code);
        
        if (code == HTTP_NOT_FOUND) {
        	S.log("S3: File not found: ");
        } 
        
        if (code != HTTP_OK) {
        	S.log("S3: Failed with message: " +  conn.getResponseMessage());
        } 
        
        BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
        InputStreamReader r = new InputStreamReader(in);
        
        
        char[] buf = new char[256];
        int read = r.read(buf);
        StringBuilder builder = new StringBuilder();
        while(read > -1) {
        	builder.append(buf, 0, read);
        	read = r.read(buf);
        }
        String jsonString = builder.toString();
        Gson gson = new Gson();
        DeepData deep = gson.fromJson(jsonString, DeepData.class);
        return deep;
	}
		
	private void printTicker() throws IOException {
		String url = URL + "/api/ticker/";
		HttpURLConnection conn = openHttpConnection(url);
		conn.setRequestMethod(GET);
        conn.setRequestProperty(ATTR_CONTENT_TYPE, "");
                   
        int code = -1;
        code = conn.getResponseCode();
        S.log("S3: Response code during downloading = " + code);
        
        if (code == HTTP_NOT_FOUND) {
        	S.log("S3: File not found: ");
        } 
        
        if (code != HTTP_OK) {
        	S.log("S3: Failed with message: " +  conn.getResponseMessage());
        } 
        
        BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
        InputStreamReader r = new InputStreamReader(in);
        
        
        char[] buf = new char[256];
        int read = r.read(buf);
        while(read > -1) {
        	S.log(new String(buf, 0, read));
        	read = r.read(buf);
        }
        
	}
	
	
	
	
	private void printDeep() throws IOException {
		
        DeepData deep = getDeep();
//        JsonParser parser = new JsonParser();
//        JsonElement element = parser.parse(jsonString);
//        element.isJsonObject()
       // S.log(deep.toString());
        S.log(DeepStringFormatter.formatHTML(deep));
//        String message = gson.fromJson(array.get(0), String.class);
//        int number = gson.fromJson(array.get(1), int.class);
//        Event event = gson.fromJson(array.get(2), Event.class);
        
	}
	
//	
//	public static String encode(String key, String data) throws Exception {
//	  Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
//	  SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
//	  sha256_HMAC.init(secret_key);
//	  return com.google.appengine.repackaged.com.google.common.util.Base64.encode(sha256_HMAC.doFinal(data.getBytes()));
//    }
//	
//	private String signature(long nonce, String cid, String apik, String s)  {
//		String sig = nonce + cid + apik;
//		String result = null;
//		try {
//			result = encode(s, sig);
//		} catch (Exception e) {
//			S.err(e);
//		}
//		
//		return result;
//	}

	

}
